﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data;
using Microsoft.Reporting.WebForms;
using Ewts.Code;

namespace Ewts.RDLC
{
    public partial class TallyPrintRDLC : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {
            if (!Page.IsPostBack)
            {
                if (Session["UserInfo"] == null)
                {
                    Response.Redirect("/accounts/login.aspx");
                }
                else
                {
                    if (Request["tallyid"] != null && Request["tallyid"].ToString().Trim() != "0")
                    {
                        int tallyid = int.Parse(Request["tallyid"].ToString().Trim());
                        DataLib.BLL.LoadNum bllLoad = new DataLib.BLL.LoadNum();
                        DataLib.BLL.Tally bllTally = new DataLib.BLL.Tally();
                        DataLib.Model.Tally modeltally = bllTally.GetModel(tallyid);
                        if (modeltally.loadid != null && modeltally.loadid != 0)
                        {
                            int loadid = (int)modeltally.loadid;
                            int proid = bllLoad.GetProidByLoadid(loadid.ToString());
                            DataLib.Model.LoadNum modelload = bllLoad.GetModel(loadid);
                            DataLib.Model.Project modelpro = new DataLib.BLL.Project().GetModel(proid);
                            //DataLib.Model.Cumtomer modelcum = new DataLib.BLL.Cumtomer().GetModel((int)modelpro.CustomerID);
                            DataLib.Model.InStorage modelIns = bllTally.GetInsID(loadid);
                            if (modelload.printname != null && modelload.printname.Trim() != "")
                            {
                                lblCustomer.Text = modelload.printname;
                            }
                            else
                            {
                                lblCustomer.Text = modelload.cumname;
                            }
                            lblProNo.Text = modelpro.ProjectNum;
                            lblProName.Text = modelpro.ProjectName;
                            //lblDateTally.Text = ((DateTime)modeltally.OperDate).ToLongDateString().ToString();
                            //lblDateCon.Text = bllTally.getWrlDate((int)modeltally.loadid,"load","M");
                        }
                        else if(modeltally.outid !=0)
                        {
                            lblProNo.Text = "移库";
                            lblProName.Text = "移库";
                            int outid = (int)modeltally.outid;
                            DataLib.BLL.OutStorage bllouts = new DataLib.BLL.OutStorage();
                            DataLib.Model.OutStorage modelouts = bllouts.GetModel(outid);
                            lblCustomer.Text = modelouts.inDepart + "伟翔";
                            //lblDateCon.Text = bllTally.getOutsInsDate(modelouts.VLoadNum);
                            //lblDateTally.Text = bllTally.getOutsInsDate(modelouts.VLoadNum);
                            //lblDateTally.Text = bllTally.getWrlDate(outid, "outs", "F");
                            //lblDateCon.Text = bllTally.getWrlDate(outid, "outs","M");
                        }

                        if (modeltally.InFDate != null && modeltally.InFDate.ToString().Trim() != "")
                            lblDateTally.Text = ((DateTime)modeltally.InFDate).ToLongDateString().ToString();
                        if (modeltally.InWrlDate != null && modeltally.InWrlDate.ToString().Trim() != "")
                            lblDateCon.Text = ((DateTime)modeltally.InWrlDate).ToLongDateString().ToString();

                        lblLoadNo.Text = modeltally.LoadNo;
                        hidLoadid.Value = modeltally.loadid.ToString();
                    }
                }
 
            }
        }

        protected void bindData(int tallyid)
        {
            List<ReportParameter> parameterList = new List<ReportParameter>();
            DataTable dt = new DataLib.BLL.Tally().GetTallyDetailByid(dropPayStyle.SelectedValue, tallyid);
    
          
                parameterList.Add(new ReportParameter("LoadNum", lblLoadNo.Text));
                parameterList.Add(new ReportParameter("Customer", lblCustomer.Text));
                parameterList.Add(new ReportParameter("ProNo", lblProNo.Text));
                parameterList.Add(new ReportParameter("CollectDate", lblDateCon.Text));    //收集称重日期
                parameterList.Add(new ReportParameter("HandleDate", lblDateTally.Text));    //分类出库日期
           
            //为列表绑定数据
            ReportDataSource rds = new ReportDataSource("DataSet1", dt);
            ReportViewer1.LocalReport.DataSources.Clear();
            ReportViewer1.LocalReport.DataSources.Add(rds);
            new RDLCPrinter().FillRow(dt, 3, 15, 15);//为列表添加空行
            ReportViewer1.LocalReport.SetParameters(parameterList);
            ReportViewer1.LocalReport.Refresh();

        }

        protected void btnPrint_Click(object sender, EventArgs e)
        {
            bindData(int.Parse(Request["tallyid"].ToString().Trim()));
        }
    }
}